//
//  TableVCVM+Params.m
//  
//  jhf
//
//  Created by Mac on 2020/7/23.
//  Copyright © 2020 jhfappkf@163.com. All rights reserved.
//

#import "TableVCVM+Params.h"

@implementation TableVCVM (Params)

/// 处理提交数据
/// @param row 处理的row
/// @param hint 是否提示
/// return 返回数据 -2  没数据  nil 没数据但是正常  dict 要提交的数据
- (id)dealRowSumbitData:(Row *)row hint:(BOOL)hint {
    NSString *dataKey = @"";
    NSString *keyValue = @"";
    
    __block NSString *placeholder = @"参数不可为空";
    
    NSMutableDictionary *data = row.model.data;
    NSMutableDictionary *additionData = row.model.additionData;
    
    NSArray *needKeys;
    NSArray *hintVals;
    
    BOOL optionData = false;
    BOOL doNotUpload = false;
    
    if ([row modelClass:RowBaseShowM.class]) { // base
        RowBaseShowM *model = row.model.model;
        dataKey = model.dataKey;
        keyValue = model.keyValue;
        optionData = model.optionData;
        doNotUpload = model.doNotUpload;
        needKeys = model.needKeys;
        hintVals = model.needKeysHintVals;
        
        if ([row modelClass:RowInputBaseM.class]) {
            RowInputBaseM *model = row.model.model;
            placeholder = model.inputControl.placeholderStr;
        }else if ([row modelClass:RowTitleBaseM.class]) {
            RowTitleBaseM *model = row.model.model;
            placeholder = model.placeholderStr;
        }
    }
    if (row.height == 0) { // 没有行高 不需要上传
        doNotUpload = true;
    }
    if (doNotUpload) { // 不需要上传
        return nil;
    }
    
    // 自定义数据
    if ((data && [data isKindOfClass:NSDictionary.class] && data.allKeys.count > 0) || (needKeys && needKeys.count > 0)) { // 自定义默认值数据 合并
        __block BOOL isReady = true;
        if (needKeys && needKeys.count > 0) { // needKeys 验证
            [needKeys enumerateObjectsUsingBlock:^(id  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
                isReady = isReady && [data.allKeys containsObject:key];
                if (!isReady) {
                    if ([hintVals safeObjectAtIndex:idx]) {
                        placeholder = [hintVals safeObjectAtIndex:idx];
                    }
                    *stop = true;
                }
            }];
        }else { // 默认验证
            [data.allValues enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                isReady = isReady && (obj.clearSpace.length > 0);
                if (!isReady) {
                    *stop = true;
                }
            }];
        }
        if (isReady) {
            if (additionData.allKeys.count > 0 ) {
                return [additionData dictionaryByMergingWith:data].mutableCopy;
            }
            return data;
        }
        if (optionData) { //是非必须传参
            return nil;
        }
        if (hint) {
            EHUD(placeholder);
        }
        return @"-2";
    }
    
    //
    if (!dataKey || dataKey.clearSpace.length <= 0) { // 没有key
        if (additionData.allKeys.count > 0 ) {
            return additionData;
        }
        return nil;
    }
    
    // 默认验证
    if (keyValue && keyValue.clearSpace.length > 0) { // 默认值
        NSMutableDictionary *temp = [NSMutableDictionary dictionary];
        [temp setValue:keyValue forKey:dataKey];
        if (additionData.allKeys.count > 0 ) {
            return [additionData dictionaryByMergingWith:temp].mutableCopy;
        }
        return temp;
    }
    if (optionData) { // 是非必须参数
        return nil;
    }else {
        if (hint) {
            EHUD(placeholder);
        }
        return @"-2";
    }
}

- (NSMutableDictionary *)checkSumbitRow; {
    __block BOOL result = true;
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            id data = [self dealRowSumbitData:row hint:true];
            if (data) {
                if ([data isKindOfClass:NSDictionary.class]) {
                    param = [param dictionaryByMergingWith:data].mutableCopy;
                }else if ([@"-2" isEqualToString:data]) {
                    result = false;
                    *rowStop = true;
                    *sectionsStop = true;
                    return;
                }
            }
        }];
    }];
    if (result) {
        return param;
    }
    return nil;
}

- (NSMutableDictionary *)checkSumbitRowOutHint {
    __block BOOL result = true;
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            id data = [self dealRowSumbitData:row hint:false];
            if (data) {
                if ([data isKindOfClass:NSDictionary.class]) {
                    param = [param dictionaryByMergingWith:data].mutableCopy;
                }else if ([@"-2" isEqualToString:data]) {
                    result = false;
                    *rowStop = true;
                    *sectionsStop = true;
                    return;
                }
            }
        }];
    }];
    if (result) {
        return param;
    }
    return nil;
}

- (NSMutableDictionary *)currentParams {
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            id data = [self dealRowSumbitData:row hint:false];
            if (data && [data isKindOfClass:NSDictionary.class]) {
               param = [param dictionaryByMergingWith:data].mutableCopy;
            }
        }];
    }];
    return param;
}

// MARK: - 当前已有图片数据
- (NSMutableDictionary <NSString *, NSArray <JAddImageModel *> *> *)currentJAddImageModels {
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            if ([row modelClass:RowUploadImagesM.class]) {
                NSMutableArray *tempArray = [NSMutableArray array];
                RowUploadImagesM *model = (RowUploadImagesM *)row.model.model;
                [model.dataArray enumerateObjectsUsingBlock:^(JAddImageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (!obj.isAddBtn) {
                        [tempArray addObject:obj];
                    }
                }];
                if (model.dataKey && model.dataKey.length > 0) {
                    [param setValue:tempArray forKey:model.dataKey];
                }
            }
        }];
    }];
    return param;
}

// MARK: - 图片文件
- (NSMutableDictionary <NSString *, NSArray <UIImage *> *> *)currentImagesFiles {
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            if ([row modelClass:RowUploadImagesM.class]) {
                NSMutableArray *tempArray = [NSMutableArray array];
                RowUploadImagesM *model = (RowUploadImagesM *)row.model.model;
                [model.dataArray enumerateObjectsUsingBlock:^(JAddImageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (!obj.isAddBtn) {
                        if (obj.imageSource) {
                            [tempArray addObject:obj.imageSource];
                        }
                    }
                }];
                if (model.dataKey && model.dataKey.length > 0) {
                    [param setValue:tempArray forKey:model.dataKey];
                }
            }
        }];
    }];
    return param;
}

- (NSMutableDictionary <NSString *, NSMutableDictionary <NSString *, UIImage *> *> *)currentImagesIdxFiles {
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            if ([row modelClass:RowUploadImagesM.class]) {
                NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
                RowUploadImagesM *model = (RowUploadImagesM *)row.model.model;
                [model.dataArray enumerateObjectsUsingBlock:^(JAddImageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (!obj.isAddBtn) {
                        if (obj.imageSource) {
                            [tempDict setValue:obj.imageSource forKey:IntToSting(idx)];
                        }
                    }
                }];
                if (model.dataKey && model.dataKey.length > 0) {
                    [param setValue:tempDict forKey:model.dataKey];
                }
            }
        }];
    }];
    return param;
}

- (NSMutableArray <UIImage *> *)currentImagesListFiles {
    NSMutableArray <UIImage *> *files = [NSMutableArray array];
    NSMutableDictionary *currentFilesDict = [self currentImagesFiles];
    [currentFilesDict.allKeys enumerateObjectsUsingBlock:^(NSString * _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *arr = ((NSArray *)[currentFilesDict valueForKey:key]);
        [files addObjectsFromArray:arr];
    }];
    return files;
}

//MARK: - 图片model
- (NSMutableDictionary <NSString *, NSArray <NSString *> *> *)currentImageContents {
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            if ([row modelClass:RowUploadImagesM.class]) {
                NSMutableArray *tempArray = [NSMutableArray array];
                RowUploadImagesM *model = (RowUploadImagesM *)row.model.model;
                [model.dataArray enumerateObjectsUsingBlock:^(JAddImageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (!obj.isAddBtn) {
                        if (obj.fileContent) {
                            [tempArray addObject:obj.fileContent];
                        }
                    }
                }];
                if (model.dataKey && model.dataKey.length > 0) {
                    [param setValue:tempArray forKey:model.dataKey];
                }
            }
        }];
    }];
    return param;
}

- (NSMutableDictionary <NSString *,  NSMutableDictionary <NSString *,  NSString *> *> *)currentImageContentIdxParams {
    __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [self.sections enumerateObjectsUsingBlock:^(Section *section, NSUInteger sectionIdx, BOOL * _Nonnull sectionsStop) {
        [section.rows enumerateObjectsUsingBlock:^(Row *row, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
            if ([row modelClass:RowUploadImagesM.class]) {
                NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
                RowUploadImagesM *model = (RowUploadImagesM *)row.model.model;
                [model.dataArray enumerateObjectsUsingBlock:^(JAddImageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (!obj.isAddBtn) {
                        if (obj.fileContent) {
                            [tempDict setValue:obj.fileContent forKey:IntToSting(idx)];
                        }
                    }
                }];
                if (model.dataKey && model.dataKey.length > 0) {
                    [param setValue:tempDict forKey:model.dataKey];
                }
            }
        }];
    }];
    return param;
}

- (NSMutableArray <NSString *> *)currentImageContentsList {
    NSMutableArray <NSString *> *files = [NSMutableArray array];
    NSMutableDictionary *currentFilesDict = [self currentImageContents];
    [currentFilesDict.allKeys enumerateObjectsUsingBlock:^(NSString * _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *arr = ((NSArray *)[currentFilesDict valueForKey:key]);
        [files addObjectsFromArray:arr];
    }];
    return files;
}

@end
